package com.qili.service.search.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.qili.base.entity.CurrentMenu;
import com.qili.base.entity.CurrentRole;
import com.qili.base.entity.CurrentUser;
import com.qili.entity.sys.SysMenu;
import com.qili.service.search.SearchIndexService;
import com.qili.service.sys.MenuService;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.security.Principal;
import java.util.List;

/**
 * @author liuyang
 * @date 2021/5/8
 * @description
 */
@Log
@Service
public class SearchIndexServiceImpl implements SearchIndexService {

    @Autowired
    private MenuService menuService;



    @Override
    public List<SysMenu> selectLeftMenus() {

        List<SysMenu> firstMenu = menuService.getSearchMenuFirst();
        Subject subject = SecurityUtils.getSubject();

        if(CollectionUtil.isNotEmpty(firstMenu)){
            for(SysMenu aFirstMenu : firstMenu){
                Example example = new Example(SysMenu.class);
                example.and().andEqualTo("pId",aFirstMenu.getId()).andEqualTo("menuType",new Byte("0"));
                example.orderBy("orderNum");
                List<SysMenu> secondMenuList = menuService.selectByExample(example);
                if(CollectionUtil.isNotEmpty(secondMenuList)){
                    for(SysMenu aSecondMenu : secondMenuList){
                        Boolean hasPermissionce = isHasPermission(aSecondMenu.getPermission());
                        if(hasPermissionce){
                            try{
                                //验证是否有权限
                                subject.checkPermission(aSecondMenu.getPermission());
                                //有权限获取其下级菜单:
                                Example thirdExample = new Example(SysMenu.class);
                                thirdExample.and().andEqualTo("pId",aSecondMenu.getId()).andEqualTo("menuType",new Byte("0"));
                                thirdExample.orderBy("orderNum");
                                List<SysMenu> thirdMenus = menuService.selectByExample(thirdExample);

                                if(CollectionUtil.isNotEmpty(thirdMenus)){
                                    //验证是否有权限,如果有权限,那么添加入二级菜单:
                                    for(SysMenu aThirdMenu : thirdMenus){
                                        try{
                                            Boolean hasPermission = isHasPermission(aThirdMenu.getPermission());
                                            if(hasPermission){
                                                aSecondMenu.addChild(aThirdMenu);
                                            }
                                        }catch (Exception e){
                                            System.out.println("没有权限=====]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]==");

                                        }
                                    }
                                }
                                aFirstMenu.addChild(aSecondMenu);

                            }catch (Exception e){
                                //说明没有该菜单的权限
                                System.out.println("没有权88888");

                            }
                        }

                    }
                }
            }
        }
        return firstMenu;
    }

    public Boolean isHasPermission(String permission){
        Boolean  ishas=false;
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //根据用户获取角色 根据角色获取所有按钮权限
        CurrentUser cUser = (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("currentPrincipal");
        for (CurrentRole cRole : cUser.getCurrentRoleList()) {
            info.addRole(cRole.getRoleName());
        }
        for (CurrentMenu cMenu : cUser.getCurrentMenuList()) {
            if (!StringUtils.isEmpty(cMenu.getPermission())) {
                if(permission.equals(cMenu.getPermission())){
                    ishas=true;
                    break;
                }
            }
        }
        return ishas;
    }
}
